package com.sumainfor.pc.gmio.service.impl;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.pc.gmio.dao.SeBillOutlayMapper;
import com.sumainfor.pc.gmio.dao.SePaymentHistoryMapper;
import com.sumainfor.pc.gmio.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.gmio.service.SeBillOutlayService;
import com.sumainfor.pc.gmio.service.SePaymentHistoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 服务秘书 业主水电燃账单
 */
@Service
public class SePaymentHistorySerivceImpl implements SePaymentHistoryService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log= LoggerFactory.getLogger(SePaymentHistorySerivceImpl.class);
    @Autowired
    private SePaymentHistoryMapper sePaymentHistoryMapper;
    @Autowired
    private ZzuulFeign zzuulFeign;
    /**
     *根据账单编号查询房源水电历史缴费信息
     */
    @Override
    public List<Map<String, Object>> getPaymentHistory(Map<String, Object> params) {
        return sePaymentHistoryMapper.getPaymentHistory(params);
    }
    /**
     *根据账单编号查询历史缴费记录关联的账单信息
     */
    @Override
    public JsonResult getPayInfoList(Map<String, Object> params) {
        JsonResult result=new JsonResult();
        if (params.get("SouId")==null){
            return result.putFailed("参数不正确");
        }
        //保存公司编号列表
        HashSet<String> areaIdList=new HashSet<String>();
        //查询账单信息的列表
        List<Map<String,Object>> gmioList=sePaymentHistoryMapper.getPayInfoList(params);
        for (Map<String,Object> gmioMap: gmioList){
            areaIdList.add(gmioMap.get("CorId").toString());
        }
        //到网关查询账单关联的支付账单信息
        params.put("areaIdList",areaIdList);
        JsonResult zuulResult=zzuulFeign.getTranInfoList(params);
        if (zuulResult.get("errorCode")!=null && zuulResult.get("errorCode").toString().equals("0")){
            List<Map<String,Object>> zuulList=(List<Map<String,Object>>)zuulResult.get("result");
            for (Map<String,Object> map1:zuulList) {
                if (map1.get("AreaId") != null) {
                    for (Map<String, Object> map2 : gmioList) {
                        if (map2.get("CorId") != null) {
                            if (map1.get("AreaId").toString().equals(map2.get("CorId").toString())) {
                                map2.putAll(map1);
                            }
                        }
                    }
                }
            }
        }
        if (gmioList!=null){
            result.put(gmioList);
        }else{
            result.putFailed("服务器繁忙");
        }
        return result;
    }
    /**
     * 租客端查询账单的历史缴费记录
     */
    @Override
    public JsonResult getPaymePrice(Map<String, Object> params) {
        JsonResult result=new JsonResult();
        if (params.get("SouId")==null){
            return result.putFailed("参数不正确");
        }
        Map<String,Object> resultMap=new HashMap<String,Object>();//返回给前端的
        BigDecimal billMoney=new BigDecimal("0");//保存历史缴费总和
        //根据账单编号查询历史缴费记录
        List<Map<String,Object>> list=sePaymentHistoryMapper.getPaymentHistory(params);
        for (Map<String,Object> map: list){
            if (map.get("PaymePrice")!=null && !String.valueOf(map.get("PaymePrice")).equals("")){
                billMoney=billMoney.add(new BigDecimal(String.valueOf(map.get("PaymePrice"))));
            }
        }
        if(list!=null){
            resultMap.put("list",list);
        }
        resultMap.put("billMoney",billMoney);
        return result.put(resultMap);
    }
}
